package py.com.fms.com.persistence.services;

import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.ibatis.sqlmap.client.SqlMapClient;

import py.com.fms.com.bean.PaymentOrderDetBean;
import py.com.fms.com.bean.PurchasesHeadBean;
import py.com.fms.com.bean.PurchasesDetBean;
import py.com.fms.exception.CommonException;
import py.com.fms.fms.bean.PersonBean;
import py.com.fms.persistence.services.FmsServices;

@SuppressWarnings("unused")
public class PurchasesServices extends FmsServices {

	public PurchasesServices(SqlMapClient client) {
		super(client);
		// TODO Auto-generated constructor stub
	}
	
	/**
	 [[[
	 [[ The following method retrieve a list of all records
	 [[ Normally used to load a grid.
	 */
	
	/**
	 * Returns an List object with all data of the Entity Header. 
	 *
	 * @return      a list with all data
	 */
	@SuppressWarnings("unchecked")
	public List<PurchasesHeadBean> getAllPurchaseHead() throws CommonException{
		List<PurchasesHeadBean> queryForList;
		try {
			
			//[[ queryForList is an implemented interface of iBatis]] 
			
			queryForList = (List<PurchasesHeadBean>)this.getConfig().queryForList("purchases.getAllPurchasesHead");
			return queryForList;
			
		} catch (SQLException e) {
			throw new CommonException(e);
		}
	}
	
	/**
	 * Returns an List of Purchases for a specify supplier. 
	 * The argument must be a Long Object with the value of the Supplier Id
	 *
	 * @param  		the supplier id
	 * @return      a list with the purchases for the passed argument (Supplier Id)
	 */
	@SuppressWarnings("unchecked")
	public List<PurchasesHeadBean> getPurchaseBySupplierId(Long supplierId) throws CommonException{
		List<PurchasesHeadBean> queryForList;
		try {
			
			//[[ queryForList is an implemented interface of iBatis]] 
			
			queryForList = (List<PurchasesHeadBean>)this.getConfig().queryForList("purchases.getPurchasesBySupplierId", supplierId);
			return queryForList;
			
		} catch (SQLException e) {
			throw new CommonException(e);
		}
	}
	
	
	/**
	 * Returns an List object with all data of the Entity Detail. 
	 *
	 * @return      a list with all data
	 */
	@SuppressWarnings("unchecked")
	public List<PurchasesDetBean> getAllPurchaseDet() throws CommonException{
		List<PurchasesDetBean> queryForList;
		try {
			
			//[[ queryForList is an implemented interface of iBatis]] 
			
			queryForList = (List<PurchasesDetBean>)this.getConfig().queryForList("purchases.getAllPurchasesDet");
			return queryForList;
			
		} catch (SQLException e) {
			throw new CommonException(e);
		}
	}
	
	/**
	 * Returns an List of Details for a Header Id. 
	 * The argument must be a Long Object with the value of the header Id
	 *
	 * @param  		value of the header id
	 * @return      a list with the details for the passed argument (header Id)
	 */
	@SuppressWarnings("unchecked")
	public List<PurchasesDetBean> getPurchaseDetByHeadId(Long headId) throws CommonException{
		List<PurchasesDetBean> queryForList;
		try {
			
			//[[ queryForList is an implemented interface of iBatis]] 
			
			queryForList = (List<PurchasesDetBean>)this.getConfig().queryForList("purchases.getPurchaseDetByHeadId", headId);
			return queryForList;
			
		} catch (SQLException e) {
			throw new CommonException(e);
		}
	}
	
	
	/**
	 * Returns an specific header object. 
	 * The argument must be a Long Object with the value of the header Id
	 *
	 * @param  		value of the header id
	 * @return      the header object for the passed argument (id)
	 */
	public PurchasesHeadBean getPurchaseHeadById(Long id) throws CommonException{
		PurchasesHeadBean queryForObject;
		try {
			
			//[[ queryForObject is an implemented interface of iBatis ]] 
			
			queryForObject = (PurchasesHeadBean)this.getConfig().queryForObject("purchases.getPurchaseHeadById", id);
			return queryForObject;
			
		} catch (SQLException e) {
			throw new CommonException(e);
		}
	}
	
	/**
	 * Returns an specific header object. 
	 * The passed arguments are:
	 * <p>
	 * - A Long object containing the value of the enterprise id
	 * - A long Object containing the purchases number
	 * 
	 * @param  		the enterprise id
	 * @param  		transaction type
	 * @param  		establishment
	 * @param  		expedition point
	 * @param  		voucher
	 * @return      the header object for the passed arguments
	 */
	public Long getPurchasesHeadIdByEntPurchasesUk(Long idEnt, Long idTypeTrans, String establishment, String expedition, String voucher, Long supplier) throws CommonException, SQLException{
		Long queryForObjectLong;
		try {
			HashMap< String, Object> param = new HashMap<String, Object>();
			param.put("idEnt", idEnt);
			param.put("idTypeTrans", idTypeTrans);
			param.put("establishment", establishment);
			param.put("expedition", expedition);
			param.put("voucher", voucher);
			param.put("supplier", supplier);
			queryForObjectLong = (Long)this.getConfig().queryForObject("purchases.getPurchasesHeadIdByEntPurchasesUk", param);
			return queryForObjectLong;
		} catch (SQLException e) {
			this.getConfig().endTransaction();
			throw new CommonException(e);
		}
	}
	
	/**
	 * Returns Pending Purchases. 
	 * The passed arguments are:
	 * <p>
	 * - A Long object containing the value of the supplier id
	 * - A String object containing the purchase status
	 * - A Date object containing the purchase date
	 * 
	 * @param  		the supplier id
	 * @param  		the purchase status
	 * @param  		the purchase Date
	 * 
	 */
	@SuppressWarnings("unchecked")
	public List<PurchasesHeadBean> getFilteredPurchases(Long idSupplier, String status, Date voucherDate) throws CommonException, SQLException{
		List<PurchasesHeadBean> queryForList;
		try {
			HashMap< String, Object> param = new HashMap<String, Object>();
			param.put("idSupplier", idSupplier);
			param.put("status", status);
			param.put("voucherDate", voucherDate);
			queryForList = (List<PurchasesHeadBean>)this.getConfig().queryForList("purchases.getFilteredPurchases", param);
			return queryForList;
		} catch (SQLException e) {
			this.getConfig().endTransaction();
			throw new CommonException(e);
		}
	}
	
	/**
	 * Returns Pending Purchases. 
	 * The passed arguments are:
	 * <p>
	 * - A Long object containing the value of the supplier id
	 * - A String object containing the purchase status
	 * - A Date object containing the purchase date
	 * 
	 * @param  		the supplier id
	 * @param  		the purchase status
	 * @param  		the purchase Date
	 * 
	 */
	@SuppressWarnings("unchecked")
	public List<PaymentOrderDetBean> getFeesToSuppliersForPayment(Long idEnt, Long idSupplier, Date startDate, Date endDate) throws CommonException{
		List<PaymentOrderDetBean> queryForList;
		try {
			System.out.println("SEARCH: SUPPLIER: "+idSupplier);
			System.out.println("SEARCH: ENT: "+idEnt);
			System.out.println("SEARCH: START DATE: "+startDate);
			System.out.println("SEARCH: END DATE: "+endDate);
			HashMap< String, Object> param = new HashMap<String, Object>();
			param.put("idEnt", idEnt);
			param.put("idSupplier", idSupplier);
			param.put("voucherStartDate", startDate);
			param.put("voucherEndDate", endDate);
			queryForList = (List<PaymentOrderDetBean>)this.getConfig().queryForList("purchases.getFeesToSuppliersForPayment", param);
			System.out.println("despues de getConfig.queryForList a getFeesToSuppliersForPayment");
			return queryForList;
		} catch (SQLException e) {
			System.out.println("en purchasesServices.error " + e.getMessage());
			throw new CommonException(e);
		}
	}
	
	/**
	 * Returns Pending purchases. 
	 * The passed arguments are:
	 * <p>
	 * - A Long object containing the value of the supplier id
	 * - A Date object containing the purchase date
	 * 
	 * @param  		the supplier id
	 * @param  		the purchase Date
	 * 
	 */
	@SuppressWarnings("unchecked")
	public List<PurchasesHeadBean> getPendingPurchases(Long idSupplier, Date voucherDate, String status) throws CommonException, SQLException{
		List<PurchasesHeadBean> queryForList;
		try {
			HashMap< String, Object> param = new HashMap<String, Object>();
			param.put("idSupplier", idSupplier);
			param.put("voucherDate", voucherDate);
			param.put("status", status);
			queryForList = (List<PurchasesHeadBean>)this.getConfig().queryForList("purchases.getPendingPurchases", param);
			return queryForList;
		} catch (SQLException e) {
			this.getConfig().endTransaction();
			throw new CommonException(e);
		}
	}
	
	/**
	 * Insert a Header Record. 
	 * The passed argument is:
	 * <p>
	 * - A entity Object containing the header Fields
	 * 
	 * @param  		the header entity bean(PurchasesHeadBean)
	 */
	public void saveNewPurchaseHead(PurchasesHeadBean purchasesHead) throws CommonException{
		try {
			this.getConfig().startTransaction();
			this.getConfig().insert("purchases.insPurchasesHead", purchasesHead);
			this.getConfig().commitTransaction();
		} catch (SQLException e) {
			e.printStackTrace();
			throw new CommonException(e);
		} finally {
			try {
				this.getConfig().endTransaction();
			} catch (SQLException e) {
				e.printStackTrace();
				throw new CommonException(e);
			}
		}
	}
	
	/**
	 * Insert Detail Record. 
	 * The passed argument is:
	 * <p>
	 * - A entity Object containing the detail fields including the header id
	 * 
	 * @param  		the detail entity bean(PurchasesDetBean)
	 */
	public void saveNewPurchaseDet(PurchasesDetBean purchasesDet) throws CommonException{
		try {
			this.getConfig().startTransaction();
			this.getConfig().insert("purchases.insPurchasesDet", purchasesDet);
			this.getConfig().commitTransaction();
		} catch (SQLException e) {
			e.printStackTrace();
			throw new CommonException(e);
		} finally {
			try {
				this.getConfig().endTransaction();
			} catch (SQLException e) {
				e.printStackTrace();
				throw new CommonException(e);
			}
		}
	}
	
	
	/**
	 * Update a Header Record. 
	 * The passed argument is:
	 * <p>
	 * - A entity Object containing the header Fields
	 * 
	 * @param  		the header entity bean(PurchasesHeadBean)
	 */
	public void updatePurchaseHead(PurchasesHeadBean purchasesHead) throws CommonException{
		try {
			this.getConfig().startTransaction();
			this.getConfig().update("purchases.udpPurchasesHeadById", purchasesHead);
			this.getConfig().commitTransaction();
		} catch (SQLException e) {
			throw new CommonException(e);
		} finally {
			try {
				this.getConfig().endTransaction();
			} catch (SQLException e) {
				e.printStackTrace();
				throw new CommonException(e);
			}
		}
	}
	
	
	/**
	 * update a Detail Record. 
	 * The passed argument is:
	 * <p>
	 * - A entity Object containing the detail fields including the header id
	 * 
	 * @param  		the detail entity bean(PurchasesDetBean)
	 */
	public void updatePurchaseDet(PurchasesDetBean purchasesDet) throws CommonException{
		try {
			this.getConfig().startTransaction();
			this.getConfig().update("purchases.udpPurchasesDetById", purchasesDet);
			this.getConfig().commitTransaction();
		} catch (SQLException e) {
			throw new CommonException(e);
		} finally {
			try {
				this.getConfig().endTransaction();
			} catch (SQLException e) {
				e.printStackTrace();
				throw new CommonException(e);
			}
		}
	}
	
	
	/**
	 * Confirm a specific Purchase. 
	 * The passed argument is:
	 * <p>
	 * - A Long object with the Purchase id
	 * 
	 * @param  		the Purchase id
	 */
	public void confirmPurchase(Long idPurchase) throws CommonException{
		try {
			this.getConfig().startTransaction();
			/*cambiar de estado a la compra*/
			this.getConfig().update("purchases.confirmPurchaseById", idPurchase);
			this.getConfig().commitTransaction();
		} catch (SQLException e) {
			throw new CommonException(e);
		} finally {
			try {
				this.getConfig().endTransaction();
			} catch (SQLException e) {
				e.printStackTrace();
				throw new CommonException(e);
			}
		}
	}
	
	/**
	 * Cancel a specific Purchase. 
	 * The passed argument is:
	 * <p>
	 * - A Long object with the Purchase id
	 * 
	 * @param  		the Purchase id
	 */
	public void cancelPurchase(Long idPurchase) throws CommonException{
		try {
			this.getConfig().startTransaction();
			this.getConfig().update("purchases.cancelPurchaseById", idPurchase);
			this.getConfig().commitTransaction();
		} catch (SQLException e) {
			throw new CommonException(e);
		} finally {
			try {
				this.getConfig().endTransaction();
			} catch (SQLException e) {
				e.printStackTrace();
				throw new CommonException(e);
			}
		}
	}

}
